package com.qb.hotelTV.base;

import androidx.lifecycle.ViewModel;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.LiveData;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

/**
 * 基础ViewModel类
 * 提供通用的功能：加载状态管理、错误处理、资源清理
 */
public abstract class BaseViewModel extends ViewModel {
    
    // 加载状态管理
    protected MutableLiveData<Boolean> _isLoading = new MutableLiveData<>();
    public LiveData<Boolean> isLoading = _isLoading;
    
    // 错误信息管理
    protected MutableLiveData<String> _errorMessage = new MutableLiveData<>();
    public LiveData<String> errorMessage = _errorMessage;
    
    // Toast消息管理
    protected MutableLiveData<String> _toastMessage = new MutableLiveData<>();
    public LiveData<String> toastMessage = _toastMessage;
    
    // RxJava订阅管理
    protected CompositeDisposable compositeDisposable = new CompositeDisposable();
    
    /**
     * 添加订阅到CompositeDisposable
     */
    protected void addDisposable(Disposable disposable) {
        compositeDisposable.add(disposable);
    }
    
    /**
     * 设置加载状态
     */
    protected void setLoading(boolean loading) {
        _isLoading.postValue(loading);
    }
    
    /**
     * 显示错误信息
     */
    protected void showError(String message) {
        _errorMessage.postValue(message);
    }
    
    /**
     * 显示Toast信息
     */
    protected void showToast(String message) {
        _toastMessage.postValue(message);
    }
    
    @Override
    protected void onCleared() {
        super.onCleared();
        // 清理所有订阅，防止内存泄漏
        if (!compositeDisposable.isDisposed()) {
            compositeDisposable.clear();
        }
    }
} 